// pages/video/video.js
import {
  groupList,
  videoListByGroup,
  getVideoUrl
} from '../../api/video/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navTabs: [],
    currentNavTab: "58100",
    videoList: [],
    currentPlayVideoId: "",
    currentPlayVideoUrl: "",
    isTriggered: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGroupList();
  },

  getGroupList: async function () {
    let groupListResp = await groupList();
    console.log(groupListResp);
    this.setData({
      navTabs: groupListResp.data.slice(0, 14),
      currentNavTab: groupListResp.data[0].id
    });

    this.getVideoList();
  },

  // 导航点击事件
  navItemClick: function (event) {
    let id = event.currentTarget.dataset.id;
    this.setData({
      currentNavTab: id,
    });
    this.getVideoList();
  },

  getVideoList: async function () {
    wx.showLoading({
      title: '正在加载...'
    });
    let videoListResp = await videoListByGroup({
      id: this.data.currentNavTab,
    })

    wx.hideLoading();
    let videoList = videoListResp.datas.map((item, index) => {
      return {
        ...item,
        id: index,
      }
    })
    this.setData({
      videoList,
      isTriggered: false,
    });
  },
  // 视频点击播放
  handlePaly: async function (event) {
    console.log(event);
    let videoId = event.target.dataset.id;
    let videoUrlreps = await getVideoUrl({
      id: videoId
    })

    this.setData({
      currentPlayVideoId: videoId,
      currentPlayVideoUrl: videoUrlreps.urls[0].url
    });

  },

  handleRefresh: function(){
    this.getVideoList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})